﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FriendlyURL.Web.Domain.Abstract;
using System.IO;

namespace FriendlyURL.Web.Domain.Impl
{
    public static class Log
    {
        private static IConfiguration _configuration;

        static Log()
        {
            _configuration = DIFactory.CreateConfigurationType();
        }
        /// <summary>
        /// Writes an Error to the log.
        /// </summary>
        /// <param name="message"></param>
        public static void Write(string message)
        {
            Write(message, "Error", null);
        }

        /// <summary>
        /// Writes a message to the log using the specified category.
        /// </summary>
        /// <param name="message"></param>
        /// <param name="category"></param>
        public static void Write(string message, string category)
        {
            Write(message, category, null);
        }

        /// <summary>
        /// Writes a message to the log using the specified category and additionalInformation
        /// </summary>
        /// <param name="message"></param>
        /// <param name="category"></param>
        /// <param name="additionalInformation"></param>
        public static void Write(string message, string category, Dictionary<string, string> additionalInformation)
        {
            try
            {
                /// TO-DO:
                /// Add functionality to connect to WCF Service that will handle all logging aspects of the application

                using (StreamWriter w = File.AppendText(_configuration.ErrorLogFilePath))
                {
                    LogText(message, w);


                    if (additionalInformation != null)
                    {
                        foreach (KeyValuePair<string, string> kvp in additionalInformation)
                        {
                            LogText("Key: " + kvp.Key + " Value: " + kvp.Value, w);
                        }
                    }


                    // Close the writer and underlying file.
                    w.Close();

                }
            }
            catch (Exception ex)
            {
                string messagea = ex.Message;
                string stack = ex.StackTrace;
                string source = ex.Source;
                string concat = "Message: " + messagea + " StackTrace: " + stack + " Source: " + source;
                throw new Exception("Error occured within Log.cs. " + concat, ex);
            }
        }

        public static void LogText(string logMessage, TextWriter w)
        {
            w.Write("\r\nLog Entry : ");
            w.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(),
                DateTime.Now.ToLongDateString());
            w.WriteLine("  :");
            w.WriteLine("  :{0}", logMessage);
            w.WriteLine("-------------------------------");
            // Update the underlying file.
            w.Flush();
        }
    }
}
